<?php

namespace App\Http\Controllers\Home\Set;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Users;
use App\Models\Home\Config;
use Conf;
use Illuminate\Support\Facades\Cache;

class ConfigController extends Controller
{
    protected $passField = [
        'title','logo','mac','customer','job','resume','resume_out','resume_pend'
    ];

    public function config_set(){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'title' => 'nullable|string',
                'logo' => 'nullable|string',
                'mac' => 'nullable|string',
                'customer' => 'nullable|string',
                'job' => 'nullable|string',
                'resume' => 'nullable|string',
                'resume_out' => 'nullable|string',
                'resume_pend' => 'nullable|string',

            ],[],[
                'title' => '公司名称',
                'logo' => '公司LOGO',
                'mac' => 'MAC防护',
                'customer' => '客户开发保护期',
                'job' => '兼职开发保护期',
                'resume' => '内部猎头简历保护期',
                'resume_out' => '兼职猎头简历保护期',
                'resume_pend' => '待入职简历保护期',
            ]);

            //数据库操作
            $config = [];
            foreach (request()->all() as $key => $val){

                $res = false;

                if (!is_numeric($val)){	$val = clean($val);}

                if(in_array($key,$this->passField) && trim($val)){

                    $config[$key] = $val;
                    $res = Config::where('name', $key)->update(['val' => $val]);
                }
            }
            if ($res !== false){

                foreach ($config as $k => $v){

                    Cache::forever($k, $v);

                }

                $return = ['code'=>1];

            }else{

                $return = ['code'=>0];

            }

        }else{

            $return = ['code'=>0,'msg'=>'非法请求'];

        }

        return $return;
    }

}